var MAX_SEARCH_TIME = 10000; //搜索控件时超时时间 http://doc.autoxjs.com/#/widgetsBasedAutomation?id=uiselectorfindonetimeout
var show = console.show;
var hide = console.hide;
console.show = function () {
  this.showing = true;
  show.apply(console, arguments);
};
console.hide = function () {
  this.showing = false;
  hide.apply(console, arguments);
};

function bezier_curves(cp, t) {
  cx = 3.0 * (cp[1].x - cp[0].x);
  bx = 3.0 * (cp[2].x - cp[1].x) - cx;
  ax = cp[3].x - cp[0].x - cx - bx;
  cy = 3.0 * (cp[1].y - cp[0].y);
  by = 3.0 * (cp[2].y - cp[1].y) - cy;
  ay = cp[3].y - cp[0].y - cy - by;

  tSquared = t * t;
  tCubed = tSquared * t;
  result = {
    x: 0,
    y: 0,
  };
  result.x = ax * tCubed + bx * tSquared + cx * t + cp[0].x;
  result.y = ay * tCubed + by * tSquared + cy * t + cp[0].y;
  return result;
}

//仿真随机带曲线滑动
//qx, qy, zx, zy, time 代表起点x,起点y,终点x,终点y,过程耗时单位毫秒
function sml_move(qx, qy, zx, zy, time) {
  var xxy = [time];
  var point = [];
  var dx0 = {
    x: qx,
    y: qy,
  };

  var dx1 = {
    x: random(qx - 100, qx + 100),
    y: random(qy, qy + 50),
  };
  var dx2 = {
    x: random(zx - 100, zx + 100),
    y: random(zy, zy + 50),
  };
  var dx3 = {
    x: zx,
    y: zy,
  };
  point.push(dx0);
  point.push(dx1);
  point.push(dx2);
  point.push(dx3);

  for (let i = 0; i < 1; i += 0.08) {
    xxyy = [
      parseInt(bezier_curves(point, i).x),
      parseInt(bezier_curves(point, i).y),
    ];

    xxy.push(xxyy);
  }

  gesture.apply(null, xxy);
}
var utils = {
  storage: {
    get() {},
    set() {},
  },
  clickOcrText(ocrText) {
    this.clickBounds(ocrText.bounds);
  },
  clickOcrItem(ocrItem) {
    this.clickBounds(ocrItem.bounds);
  },
  getOcrItemPoint(ocrItem) {
    var bounds = ocrItem.bounds;
    var left = bounds.left;
    var right = bounds.right;
    var top = bounds.top;
    var bottom = bounds.bottom;
    return {
      x: random(left + 1, right - 1),
      y: random(top + 1, bottom - 1),
    };
  },
  clickUIControl(UIControl) {
    //点击ui控件
    while (true) {
      if (UIControl && UIControl.clickable()) {
        UIControl.click();
        return true;
      } else if (UIControl) {
        UIControl = UIControl.parent();
      } else {
        return false;
      }
    }
  },
  clickUIText(textUI) {
    var bounds = textUI.bounds();
    this.clickBounds(bounds);
  },

  getUITextPoint(UIText) {
    return {
      x: UIText.bounds().centerX(),
      y: UIText.bounds().centerY(),
    };
  },
  existsTextStartsWith(content, ignoreLog) {
    if (textStartsWith(content).exists()) {
      var UIText = textStartsWith(content).findOne(MAX_SEARCH_TIME);
      if (!ignoreLog && UIText) {
        console.log(
          "existsTextStartsWith",
          "找到:",
          content,
          "/",
          UIText.text()
        );
      }
      return UIText;
    } else {
      return false;
    }
  },
  existsText(content, ignoreLog) {
    if (text(content).exists()) {
      var UIText = text(content).findOne(MAX_SEARCH_TIME);
      if (!ignoreLog && UIText) {
        console.log("existsText", "找到:", content, "/", UIText.text());
      }
      return UIText;
    } else {
      return false;
    }
  },
  clickText(content, ignoreLog) {
    if (this.existsText(content, true)) {
      var UIText = text(content).findOne(MAX_SEARCH_TIME);
      var bounds = UIText.bounds();
      if (!ignoreLog) {
        console.log("clickText", "找到:", content, "/", UIText.text());
      }
      this.clickBounds(bounds);
      return UIText;
    } else {
      return false;
    }
  },
  existsOneTextStartsWith(content, ignoreLog) {
    if (!Array.isArray(content)) {
      content = [content];
    }
    for (var index = 0; index < content.length; index++) {
      var item = content[index];
      var result = this.existsTextStartsWith(item, true);
      if (!ignoreLog && result) {
        console.log(
          "existsOneTextStartsWith",
          "找到:",
          item,
          "/",
          result.text()
        );
        return result;
      }
    }
  },
  clickTextStartsWith(content, ignoreLog) {
    var UIText = this.existsTextStartsWith(content, true);
    if (UIText) {
      var bounds = UIText.bounds();
      if (!ignoreLog) {
        console.log(
          "clickTextStartsWith",
          "找到:",
          content,
          "/",
          UIText.text()
        );
      }
      this.clickBounds(bounds);
      return UIText;
    } else {
      return false;
    }
  },
  existsTextEndsWith(content, ignoreLog) {
    if (textEndsWith(content).exists()) {
      var UIText = textEndsWith(content).findOne(MAX_SEARCH_TIME);
      if (!ignoreLog && UIText) {
        console.log(
          "existsTextEndsWith",
          "找到：",
          content,
          "/",
          UIText.text()
        );
      }
      return UIText;
    } else {
      return false;
    }
  },
  clickTextEndsWith(content, ignoreLog) {
    var UIText = this.existsTextEndsWith(content, true);
    if (UIText) {
      var bounds = UIText.bounds();
      if (!ignoreLog) {
        console.log("clickTextEndsWith", "找到：", content, "/", UIText.text());
      }
      this.clickBounds(bounds);
      return UIText;
    } else {
      return false;
    }
  },
  existsTextMatches(content, ignoreLog) {
    if (textMatches(content).exists()) {
      var UIText = textMatches(content).findOne(MAX_SEARCH_TIME);
      if (!ignoreLog) {
        console.log("existsTextMatches", "找到：", content, "/", UIText.text());
      }
      return UIText;
    } else {
      return false;
    }
  },
  clickTextMatches(content, ignoreLog) {
    var UIText = this.existsTextMatches(content, true);
    if (UIText) {
      if (!ignoreLog) {
        console.log("clickTextMatches", "找到：", content, "/", UIText.text());
      }
      var bounds = UIText.bounds();
      this.clickBounds(bounds);
      return UIText;
    } else {
      return false;
    }
  },
  clickBounds(bounds) {
    var left = bounds.left;
    var right = bounds.right;
    var top = bounds.top;
    var bottom = bounds.bottom;
    return this.clickXY(
      random(left + 1, right - 1),
      random(top + 1, bottom - 1)
    );
  },
  clickPoint(x1, y1, x2, y2) {
    var x = utils.getRandom(x1 + 1, x2 - 1);
    var y = utils.getRandom(y1 + 1, y2 - 1);
    this.clickXY(x, y);
  },
  clickXY(x, y) {
    console.log("clickXY", x, y);
    return click(x, y);
  },
  getSizePoint(points, w, h) {
    var length = points.length;
    var sizePoints = [];
    var rateX = device.width / (w || 720);
    var rateY = device.height / (h || 1650);
    for (var i = 0; i < length; i++) {
      var point = points[i];
      var rate = i % 2 == 0 ? rateX : rateY;
      sizePoints.push(parseInt(point * rate));
    }
    return sizePoints;
  },
  getNumber(content) {
    var REGEXP = /(\d+)/g;
    var match = content.match(REGEXP);
    return parseInt(match ? match[0] : 0);
  },
  clickPointMap(pointMap) {
    var points = pointMap.points;
    var size = pointMap.size;
    this.clickPoint.apply(this, this.getSizePoint(points, size.x, size.y));
  },
  getRandom(start, end, times) {
    var times = times || 1;
    return random(start * times, end * times);
  },
  createLogger() {
    var arguments1 = [];
    var length = arguments.length;
    for (var index = 0; index < length; index++) {
      arguments1.push(arguments[index]);
    }
    return function () {
      var arguments2 = [];
      var length = arguments.length;
      for (var index = 0; index < length; index++) {
        arguments2.push(arguments[index]);
      }
      console.log.apply(console.log, arguments1.concat(arguments2));
    };
  },
  createLoggerX(prefix, count) {
    var arguments1 = [prefix];
    var execCount = 0;
    count = count || 1;
    return function () {
      var arguments2 = [];
      var length = arguments.length;
      for (var index = 0; index < length; index++) {
        arguments2.push(arguments[index]);
      }
      execCount = execCount + 1;
      if (execCount % count == 0) {
        console.log.apply(console.log, arguments1.concat(arguments2));
      }
    };
  },
  randomIndexArray(max) {
    var result = [];
    while (result.length < max) {
      var number = random(0, max - 1);
      if (result.indexOf(number) == -1) {
        result.push(number);
      }
    }
    return result;
  },
  swipe(a, b, c, d) {
    swipe(
      device.width * a * this.getRandom(95, 5, 1),
      device.height * b * this.getRandom(95, 5, 1),
      device.width * c * this.getRandom(95, 5, 1),
      device.height * d * this.getRandom(95, 5, 1),
      utils.getRandom(2, 8, 100)
    );
  },
  swipeX(qx, qy, zx, zy, time) {
    sml_move(qx, qy, zx, zy, time);
  },
  strFormat(str, str2) {
    if (!str) {
      return "";
    }
    str2 = str2 || "哈";
    return str.split("").join(str2);
  },
  merge(a, b, c) {
    for (var prop in c) {
      a[prop] = b[prop] || c[prop];
    }
  },
  getStorage(key) {
    return storages.create(key);
  },
};

module.exports = utils;
